//
//  YLRegularFinancingListViewController.m
//  YLFinanceService
//
//  Created by she1323 on 2017/9/13.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLRegularFinancingListViewController.h"
#import "YLRegularFinancingTableViewCell.h"
#import "YLRegularFinancingDetailController.h"
#import "YLGlobeHeader.h"
#import "YLRegularFinancingListViewModel.h"
#import <MJRefresh.h>
#import "YLContinuationOrderModel.h"
@interface YLRegularFinancingListViewController ()

@property (nonatomic, strong) YLRegularFinancingListViewModel *viewModel;

@property (nonatomic, assign) NSInteger index;
// d33e11
@end

@implementation YLRegularFinancingListViewController

- (YLRegularFinancingListViewModel *)viewModel
{
    if (!_viewModel) {
        _viewModel = [[YLRegularFinancingListViewModel alloc] init];
    }
    return _viewModel;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%zd", self.type);
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([YLRegularFinancingTableViewCell class]) bundle:nil] forCellReuseIdentifier:[YLRegularFinancingTableViewCell cellID]];
    
    __weak typeof(self) weakSelf = self;
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.index = 1;
        [weakSelf loadData];
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.index ++;
        [weakSelf loadData];
    }];
    self.tableView.mj_footer.hidden = YES;
    
    [self.tableView.mj_header beginRefreshing];
    
    
    
    
    
}

- (void)loadData
{
    __weak typeof(self) weakSelf = self;
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:3];
    params[@"IsValidity"] = @(_type);
    params[@"IsVerifying"] = @(_verifying);
    params[@"PageIndex"] = @(_index);

    
    [[self.viewModel.requestCommand execute:params] subscribeNext:^(id  _Nullable x) {
        [weakSelf.tableView reloadData];
        [weakSelf noData];
    }];
    
    [self.viewModel.wh_loadDataErrorSubject subscribeNext:^(id  _Nullable x) {
        [weakSelf noData];
    }];
    
    [self.viewModel.wh_noMoreDataSubject subscribeNext:^(id  _Nullable x) {
        [weakSelf noData];
        [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
    }];
    
}

- (void)noData
{
    self.tableView.mj_footer.hidden = NO;
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
}



- (instancetype)init
{
    return [self initWithStyle:UITableViewStyleGrouped];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.viewModel.models.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YLRegularFinancingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[YLRegularFinancingTableViewCell cellID]];
    cell.model = self.viewModel.models[indexPath.section];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.0001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_canJump == NO) {
        return;
    }
    
    YLRegularFinancingDetailController *detail = [[YLRegularFinancingDetailController alloc] init];
    YLContinuationOrderModel *model = self.viewModel.models[indexPath.section];
    detail.title = model.productName;
    detail.contractCode = model.contractCode;
    [self.navigationController pushViewController:detail animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
